
/**********************************************************************************\
** 文件名称: snake.h
** 创建人员: YCG
** 创建日期: 2019-01-20
** 文档描述: 
** 版本代号: V0.1
** 版本说明: 初始版本
\********************************************************************************/
#ifndef __SNAKE_H_
#define __SNAKE_H_

#include "main.h"
#include "mycmd.h"
#include <stdlib.h>

#define SNAKE_DEBUG
#ifdef  SNAKE_DEBUG
#include "uart.h"
#define  SNAKE_Printf(fmt,...)   Uprintf(fmt,__VA_ARGS__)
#else
#define  SNAKE_Printf(...)
#endif

#define SNAKE_LEVEL_ADD   0xFF

#define Dot_IsNotEmpty(ySnakedot) ((*((uint32_t *)&ySnakedot))!=0U)
#define Dot_IsEmpty(ySnakedot)    ((*((uint32_t *)&ySnakedot))==0U)
#define Dot_Clear(ySnakedot)      ((*((uint32_t *)&ySnakedot))=0U)

#define SNAKE_OK       0
#define SNAKE_BACK     1
#define SNAKE_UP       2
#define SNAKE_DOWM     3
#define SNAKE_LEFT     4
#define SNAKE_RIGHT    5

#define SNAKE_Exit        0       //退出游戏
#define SNAKE_NO_Init     1       //未初始化
#define SNAKE_WaitStart   2       //等待进入游戏
#define SNAKE_Runing      3       //游戏运行中
#define SNAKE_UpLevel     4       //关卡升级 
#define SNAKE_GameOver    5       //游戏失败
#define SNAKE_Complete    6       //游戏通关
#define SNAKE_WaitEnd     7       //等待确认结束游戏游戏

#define SNAKE_ShowBegin        0
#define SNAKE_ShowWaitStart    1
#define SNAKE_ShowLevel        2
#define SNAKE_ShowUpLevel      3
#define SNAKE_ShowAddNewHead   4
#define SNAKE_ShowDelEnd       5
#define SNAKE_ShowComplete     6
#define SNAKE_ShowOver         7
#define SNAKE_ShowWaitEnd      8

#define SNAKE_FALSE    0
#define SNAKE_TRUE     1

#define SNAKE_MaxNum   20
#define SNAKE_MaxLevel 12

#define SNAKE_RunMove     0
#define SNAKE_RunEatfood  1
#define SNAKE_RunOver     2

typedef struct SNAKE_DOT_T {

    uint8_t Pre;      //上一个节点
    uint8_t Next;     //下一个节点
    uint8_t x;        //x坐标
    uint8_t y;        //y坐标

} Snakedot_t;

typedef struct SNAKE_T {

    uint8_t Head;     //头部
    uint8_t End;      //尾部
    uint8_t Num;      //当前总数
    uint8_t Dire;     //方向

    uint8_t Width;    //宽度
    uint8_t High;     //高度
    uint8_t State;    //状态
    uint8_t Level;    //难度等级

    uint8_t FoodX;    //食物坐标x
    uint8_t FoodY;    //食物坐标y
    uint8_t NextX;    //移动下一个点坐标x
    uint8_t NextY;    //移动下一个点坐标y
	
    uint8_t MaxLength;  //最大长度
    uint8_t MaxLevel;   //最大等级
    uint8_t MaxScore;   //分数
    uint8_t Score;      //当前分数
    
    uint8_t OldDire;     //旧方向
		
		void (*Show)(struct SNAKE_T *ySnakeHead,uint8_t State);   //绘图函数
		
    CmdHead_t Cmd[1];  //添加控制
	  Snakedot_t Dot[SNAKE_MaxNum];  //蛇身点的缓存空间

} SnakeHead_t;




void Snake_Init(SnakeHead_t* ySnakeHead,void (*yShow)(SnakeHead_t *ySnakeHead,uint8_t State), uint8_t yWidth, uint8_t yHigh, uint8_t yMaxLength);
void Snake_Start(SnakeHead_t* ySnakeHead);
void Snake_SetLevel(SnakeHead_t* ySnakeHead, uint8_t yLevel);
uint8_t Snake_IsInRang(SnakeHead_t* ySnakeHead, uint8_t x, uint8_t y);
uint8_t Snake_TryMoveStep(SnakeHead_t* ySnakeHead);
void Snake_CreatFood(SnakeHead_t* ySnakeHead);
void Snake_AddHeadDot(SnakeHead_t* ySnakeHead);
void Snake_DelEndDot(SnakeHead_t* ySnakeHead) ;

#endif
/********************************End of File************************************/

